//
//  MainVC.m
//  RRRRR
//
//  Created by why on 15-3-27.
//  Copyright (c) 2015年 why. All rights reserved.
//

#import "MainVC.h"
#define kPhotoRoot @"Photos"

@interface MainVC ()

@property (nonatomic, strong) iCarousel * photoView;

@property (nonatomic, strong) NSUserDefaults * userDefaults;

@property (nonatomic, strong) NSMutableArray * photoArray;


- (void) setupPhotoStores;

- (NSMutableArray *) photosFromBundle;

@end


@implementation MainVC



//- (void)viewWillAppear:(BOOL)animated
//{
//    animated = YES;
//    [self.userDefaults objectForKey:@"Photos"];
//    
//    @"55.jpg"
//    
//    
//    self.photoArray = [self.userDefaults objectForKey:@"99"];
//       NSLog(@"*********%@", self.photoArray);
//}

- (void)loadView
{
    [super loadView];
    [self setupPhotoStores];
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
   

    
    self.photoView = [[iCarousel alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+64, self.view.bounds.size.width, self.view.bounds.size.height / 3)];
    self.photoView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    self.photoView.delegate = self;
    self.photoView.dataSource = self;
    
    UIImageView *bbb = [[UIImageView alloc] init];
    [bbb setImageWithURLStr:@"/Users/dlios/DLS141226Team01/USB_Reader/USB_Reader/ImageSources/5454.jpg"];
    
    self.photoView.backgroundColor = [UIColor colorFromHexCode:@"#00F5FF"];
    
    self.photoView.type = iCarouselTypeCylinder;
    [self.view addSubview:self.photoView];
    
   
}


- (void) viewDidUnload {
    [super viewDidUnload];
    self.photoView = nil;
}

- (void) setupPhotoStores {

    if ([self.userDefaults objectForKey:kPhotoStore]) {
        self.photoArray = [self.userDefaults objectForKey:kPhotoStore];
        NSLog(@"%@", self.photoArray);
    } else {
        self.photoArray = [self photosFromBundle];
    }
}




- (NSMutableArray *) photosFromBundle {
    

    
    NSMutableArray * array = [NSMutableArray arrayWithObjects:@"12.jpg",@"15.jpg", @"9.jpg", @"10.jpg", @"5.jpg",nil];
    
    
    NSMutableArray *arr = [NSMutableArray array];
    for (NSString *str in array) {
        NSString *path = [[NSBundle mainBundle] pathForResource:str ofType:nil];
        [arr addObject:path];
    }
 
    [[NSUserDefaults standardUserDefaults] setValue:arr forKey:kPhotoStore];
    [[NSUserDefaults standardUserDefaults] synchronize];
    return arr;
}


/////////////////

- (void) viewLargePhoto:(UIPinchGestureRecognizer *) sender {
    UIImageView * imageView = (UIImageView *) sender.view;
    CGFloat rate = sender.scale;
    imageView.transform = CGAffineTransformMakeScale(rate, rate);
}


- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
    return self.photoArray.count;
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index {
    NSString * name = [self.photoArray objectAtIndex:index];
    UIImage * image = [UIImage imageWithContentsOfFile:name];
    UIImageView * photoView = [[UIImageView alloc] initWithImage:image];
    UIGestureRecognizer * gesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(viewLargePhoto:)];
    [photoView addGestureRecognizer:gesture];
    photoView.contentMode = UIViewContentModeScaleToFill;
    photoView.frame = CGRectMake(10, 10, 180, 200);
    return photoView;
}

- (NSUInteger) numberOfPlaceholdersInCarousel:(iCarousel *)carousel {
    return 10;
}

- (NSUInteger) numberOfVisibleItemsInCarousel:(iCarousel *)carousel {
    return 5;
}

- (BOOL)carouselShouldWrap:(iCarousel *)carousel {
    return YES;
}

- (void) carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
    UIImageView * imageView = (UIImageView *) [carousel itemViewAtIndex:index];
    [SImageViewer viewWithImage:imageView.image];
}

@end
